package com.bruce.codeFactory.utils;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author luotuan
 * @Description  Gson工具类
 * @create 2019-05-07 10:53
 **/
public class GsonUtil {
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static Gson gson;
    private static final JsonParser jsonParser = new JsonParser();
    static {
        gson = new GsonBuilder().serializeNulls().setDateFormat(DEFAULT_DATE_FORMAT).setPrettyPrinting().create();
    }

    private GsonUtil(){
        throw new AssertionError();
    }

    private static Gson builderGson(String formatDate){
        if (formatDate == null || "".equals(formatDate)) {
            formatDate = DEFAULT_DATE_FORMAT;
        }
        return new GsonBuilder().serializeNulls().setDateFormat(formatDate).setPrettyPrinting().create();
    }

    /**
     * object转json字符串
     *
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        return gson.toJson(object);
    }

    public static String toJson(String formatDate,Object object) {
        Gson newGson = builderGson(formatDate);
        return newGson.toJson(object);
    }

    /**
     * json字符串转bean
     *
     * @param paramJson
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T fromJson(String paramJson, Class<T> clazz) {
        return gson.fromJson(paramJson, clazz);
    }

    /**
     * @param paramJson
     * @param type  指定类型
     * @param <T>
     * @return
     */
    public static <T> T fromJson(String paramJson, Type type) {
        return gson.fromJson(paramJson, type);
    }

    /**
     * 将对象实例转化为所需要的class实例
     * @param instance 源对象
     * @param clazz  转换的对象
     * @param <T>
     * @return
     */
    public static <T> T convert(Object instance,Class<T> clazz){
        if(instance!=null && clazz!=null){
            String json = toJson(instance);
            if(json!=null && !"".equals(json)){
                return gson.fromJson(json,clazz);
            }
            return null;
        }
        return null;
    }

    /**
     * 将对象实例转化为所需要的Type
     * @param instance
     * @param typeOfT
     * @param <T>
     * @return
     */
    public static <T> T convert(Object instance,Type  typeOfT){
        if(instance!=null && typeOfT!=null){
            String json = toJson(instance);
            if(json!=null && !"".equals(json)){
                return (T)gson.fromJson(json,typeOfT);
            }
            return null;
        }
        return null;
    }

    /**
     * 转为List
     * @param paramJson
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> toObjects(String paramJson, Class<T> clazz) {
        return gson.fromJson(paramJson, new TypeToken<List<T>>() {}.getType());
    }

    /**
     * 转为JsonObject
     * @param jsonString
     * @return
     * @throws JsonSyntaxException
     * @throws IllegalStateException
     */
    public static JsonObject toJsonObject(String jsonString) throws JsonSyntaxException, IllegalStateException{
        return jsonParser.parse(jsonString).getAsJsonObject();
    }

    /**
     * 转为JsonArray
     * @param jsonString
     * @return
     * @throws JsonSyntaxException
     * @throws IllegalStateException
     */
    public static JsonArray toJsonArray(String jsonString) throws JsonSyntaxException, IllegalStateException {
        return jsonParser.parse(jsonString).getAsJsonArray();
    }

    /**
     * 转为JsonElement
     * @param jsonString
     * @return
     * @throws JsonSyntaxException
     * @throws IllegalStateException
     */
    public static JsonElement toJsonElement(String jsonString) throws JsonSyntaxException, IllegalStateException {
        return jsonParser.parse(jsonString);
    }

    /**
     * 是否是JsonObject
     * @param jsonString
     * @return
     */
    public static boolean isJsonObject(String jsonString) {
        try {
            return toJsonObject(jsonString).isJsonObject();
        } catch (IllegalStateException e) {
            return false;
        }
    }

    /**
     * 是否为JsonArray
     * @param jsonString
     * @return
     */
    public static boolean isJsonArray(String jsonString) {

        try {
            return toJsonArray(jsonString).isJsonArray();
        } catch (IllegalStateException e) {
            return false;
        }
    }
}
